package com.dx.mes.common.dao.impl;

import com.dx.common.table.ppt.*;
import com.dx.mes.common.core.dao.impl.GenericHibernateDao;
import com.dx.mes.common.dao.IBisStockerDao;
import com.dx.mes.common.dao.IRetStockerPtnDao;
import com.dx.mes.common.dao.IRetToolPtDao;
import com.dx.mes.common.dao.IRetToolPtPtnDao;
import com.dx.mes.common.table.rtnBean.SimpleRtnBeen;
import com.dx.mes.common.table.rtnBean.rtd.SelectStockerPatiBeen;
import com.dx.mes.common.table.rtnBean.rtd.StockerBeen;
import com.dx.mes.common.transcoder.util.Util;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

import static com.dx.mes.common.bean.def.GenericDef.*;

@Repository
public class RetStockerPtnHibernateDao extends GenericHibernateDao<Ret_stocker_ptn, Ret_stocker_ptnId>
implements IRetStockerPtnDao {
	private Logger logger = Logger.getLogger(RetStockerPtnHibernateDao.class);
	
	@Autowired
	private IRetToolPtPtnDao retToolPtPtnDao;
	@Autowired
	private IBisStockerDao bisStockerDao;
	@Autowired
	private IRetToolPtDao retToolPtDao;
	
/*	public List<StockerPatiBeen> comStkInSumPatiInCrr(String stocker_id, List<Ret_box_xfr> box_xfrList){
		List<StockerPatiBeen> stkPatiList = new ArrayList<StockerPatiBeen>();
		//获取Stocker Partition设定
		String hql_stocker_ptn = "FROM Ret_stocker_ptn WHERE stocker_id_fk='" + stocker_id + "'";
		List<Ret_stocker_ptn> stocker_ptnList = this.find(hql_stocker_ptn);
			
		for(Ret_stocker_ptn sorcker_ptn:stocker_ptnList){
			StockerPatiBeen stkPati = new StockerPatiBeen();
			Integer pati_use_cnt = 0;
			stkPati.setPati_id(sorcker_ptn.getId().getPati_id());
			stkPati.setPati_max_qty(sorcker_ptn.getPati_max_cnt() != null ? sorcker_ptn.getPati_max_cnt() : 0);
			//统计in partition count
			for(Ret_box_xfr box_xfr:box_xfrList){
				if(RTD_XFR_CMD_FLG_STI.equals(box_xfr.getXfr_cmd_flg())){
					if(sorcker_ptn.getId().getStocker_id_fk().equals(box_xfr.getXfr_pos_id()) &&
							sorcker_ptn.getId().getPati_id().equals(box_xfr.getXfr_pati_id())){
						pati_use_cnt ++;
					}
				}else if(RTD_XFR_CMD_FLG_SXS.equals(box_xfr.getXfr_cmd_flg()) ||
						RTD_XFR_CMD_FLG_TXT.equals(box_xfr.getXfr_cmd_flg())){
					if(sorcker_ptn.getId().getStocker_id_fk().equals(box_xfr.getXfr_pos_id()) && 
							sorcker_ptn.getId().getPati_id().equals(box_xfr.getXfr_pati_id())){
						pati_use_cnt ++;
					}else if(sorcker_ptn.getId().getStocker_id_fk().equals(box_xfr.getXfr_to_tool_id()) && 
							sorcker_ptn.getId().getPati_id().equals(box_xfr.getXfr_to_pati_id())){
						pati_use_cnt ++;
					}
				}
			}
			stkPati.setPati_use_qty(pati_use_cnt);
			stkPatiList.add(stkPati);
		}
		return stkPatiList;
	}*/
	
	/**
	 * Select Suitable Stocker Partition For Carrier
	 */
	public SimpleRtnBeen comStkInSelectReturnPati(List<StockerBeen> stockerBeenList, String tool_id,
                                                  String port_id, String zone_id){
		Integer ix = 0;
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		SelectStockerPatiBeen selectStockerPatiBeen = new SelectStockerPatiBeen();
		
		//Get port-partition setting(RET_TOOL_PT_PTN)
		List<Ret_tool_pt_ptn> tool_pt_ptnList = retToolPtPtnDao.find("FROM Ret_tool_pt_ptn WHERE tool_id_fk='" + tool_id
				+ "' AND port_id='" + port_id 
				+ "' AND lu_mode='" + RTD_LU_MODE_UNLD 
				+ "' ORDER BY pati_prty ASC");
		for(ix=0; ix<tool_pt_ptnList.size(); ix++){
			Ret_tool_pt_ptn tool_pt_ptn = tool_pt_ptnList.get(ix);
			rtnBeen = bisStockerDao.comStkInCheckStcUsage(stockerBeenList, tool_pt_ptn.getId().getStocker_id(),
					tool_pt_ptn.getId().getPati_id(), zone_id);
			if(rtnBeen.getlRc() == _NORMAL){
				//Get Suitable Stocker Partition For Carrier
				selectStockerPatiBeen.setStocker_id(tool_pt_ptn.getId().getStocker_id());
				selectStockerPatiBeen.setPati_id(tool_pt_ptn.getId().getPati_id());
				return new SimpleRtnBeen(selectStockerPatiBeen);
			}else{
				logger.error("Error Message[" + rtnBeen.getlRc() + "][" + rtnBeen.getMsgBuf() + "]");
			}
		}
		
		if(ix > 0){
			return new SimpleRtnBeen(E_COMMON_RTD_NO_SELECT_PARTITION,
					"No Selectable Partition for[" + tool_id + "][" + port_id + "]#1");
		}
		
		//未设定port-partition时，获取Stocker Partition信息(RET_STOCKER_PTN)
		Ret_tool_ptId tool_ptId = new Ret_tool_ptId();
		tool_ptId.setTool_id_fk(tool_id);
		tool_ptId.setPort_id(port_id);
		Ret_tool_pt ret_tool_pt = retToolPtDao.get(tool_ptId);
		if(ret_tool_pt == null){
			return new SimpleRtnBeen(E_RET_TOOL_PT + E_READ_NOT_FOUND,
					"未找到设备[" + tool_id
					+ "],端口[" + port_id
					+ "]信息[RET_TOOL_PT]");
		}else if(Util.isSpaceCheck(ret_tool_pt.getTo_stk_id())){
			return new SimpleRtnBeen(E_COMMON_RTD_IS_SPACE,
					"设备[" + tool_id
					+ "],端口[" + port_id
					+ "]未维护返回Stocker[RET_TOOL_PT][TO_STK_ID]");
		}
		rtnBeen = this.comStkInSelectPati(stockerBeenList, ret_tool_pt.getTo_stk_id(), zone_id);
		if(rtnBeen.getlRc() != _NORMAL){
			return rtnBeen;
		}else{
			selectStockerPatiBeen = (SelectStockerPatiBeen) rtnBeen.getObj();
			return new SimpleRtnBeen(selectStockerPatiBeen);
		}
	}
	
	/**
	 * 获取Stocker Partition信息
	 */
	public SimpleRtnBeen comStkInSelectPati(List<StockerBeen> stockerBeenList, String stocker_id, String zone_id){
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		
		Bis_stocker bis_stocker = bisStockerDao.get(stocker_id);
		if(bis_stocker != null){
			//获取Stocker Partition设定
			List<Ret_stocker_ptn> stocker_ptnList = this.find("FROM Ret_stocker_ptn WHERE stocker_id_fk='"
					+ stocker_id + "'");
			if(stocker_ptnList.size() == 0){
				return new SimpleRtnBeen(E_RET_STOCKER_PTN + E_READ_NOT_FOUND,
						"未找到Stocker[" + stocker_id + "]的Partition信息[RET_STOCKER_PTN]");
			}
			for(Ret_stocker_ptn stocker_ptn:stocker_ptnList){
				rtnBeen = bisStockerDao.comStkInCheckStcUsage(stockerBeenList, stocker_ptn.getId().getStocker_id_fk(),
						stocker_ptn.getId().getPati_id(), zone_id);
				if(rtnBeen.getlRc() == _NORMAL){
					//Get Suitable Stocker Partition For Carrier
					SelectStockerPatiBeen selectStockerPatiBeen = new SelectStockerPatiBeen();
					selectStockerPatiBeen.setStocker_id(stocker_ptn.getId().getStocker_id_fk());
					selectStockerPatiBeen.setPati_id(stocker_ptn.getId().getPati_id());
					return new SimpleRtnBeen(selectStockerPatiBeen);
				}else{
					logger.error("Error Message[" + rtnBeen.getlRc() + "][" + rtnBeen.getMsgBuf() + "]");
				}
			}
			
			return new SimpleRtnBeen(E_COMMON_RTD_NO_SELECT_PARTITION,
					"No Selectable Partition of[" + stocker_id + "]#2");
		}
		
		//TODO 预留 获取Stocker Group
		return new SimpleRtnBeen(_NORMAL);
	}
}
